package com.htt.app.cuasotinhyeu;

import java.util.List;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;
import com.htt.app.cuasotinhyeu.clock.Clock;
import com.htt.app.cuasotinhyeu.object.Show;
import com.htt.app.cuasotinhyeu.sqlite.DAOShow;
import com.htt.app.cuasotinhyeu.view.CustomView;
import com.htt.app.cuasotinhyeu.view.FavoriteView;
import com.htt.app.cuasotinhyeu.view.PlayerView;
import com.htt.app.cuasotinhyeu.view.ShowView;

public class CuaSoTinhYeu extends StaticActivity implements OnClickListener {
	public static Activity ctx;
//	private final String TAG = "QuickSnow";
	
	public static final int VIEWPLAYER = 0;
	public static final int VIEWSHOW = 1;
	public static final int VIEWFAVOURITE = 2;
	public static final int VIEWCUSTOM = 3;
	
	
	/* 
	 * 0 - player
	 * 1 - all channel
	 * 2 - categories
	 * 3 - favourite
	 * 4 - custom
	 */
	
	public static int viewDisplay = 0;
	
	// footer
	private static TextView home_footer_show;
	private static TextView home_footer_favourie;
	private static TextView home_footer_custom;
	
	// main layout
	public static LinearLayout home_view;
	
	public static PlayerView playerView;
	public static ShowView showView;
	public static CustomView customView;
	public static FavoriteView favouriteView;
	
	private List<Show> listFavourite = null;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ctx = this;
		
		setContentView(R.layout.home);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		
		initLayout();
		
		home_view.addView(playerView.createView(this, null));
		
	}
	
	private void initLayout() {
		
		playerView = new PlayerView();
		showView = new ShowView();
		customView = new CustomView();
		favouriteView = new FavoriteView();
		
		home_view = (LinearLayout) findViewById(R.id.home_view);
		
		home_footer_show = (TextView) findViewById(R.id.home_footer_tv_show);
		home_footer_favourie = (TextView) findViewById(R.id.home_footer_favourie);
		home_footer_custom = (TextView) findViewById(R.id.home_footer_tv_custom);
		
		// hanler footer
		home_footer_show.setOnClickListener(this);
		home_footer_favourie.setOnClickListener(this);
		home_footer_custom.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		
		case R.id.home_footer_tv_show:
			home_view.removeAllViews();

			changeStatusViewDisplay(VIEWSHOW);
			if(!showView.isCreate){
				home_view.addView(showView.createView(this));
			} else {
				
				home_view.addView(showView.getView());
			}
			
			break;
			
		
		case R.id.home_footer_favourie:
			home_view.removeAllViews();
			
			changeStatusViewDisplay(VIEWFAVOURITE);
			
			DAOShow db = new DAOShow(this);
			db.open();
			listFavourite = db.getAllShow();
			db.close();
					
			home_view.addView(favouriteView.createView(this, listFavourite));
			favouriteView.setTitle(getResources().getString(R.string.title_favourite));
			
			break;
			
		case R.id.home_footer_tv_custom:
			home_view.removeAllViews();
			
			changeStatusViewDisplay(VIEWCUSTOM);
			if(!customView.isCreate) {
				home_view.addView(customView.createView(this));
			} else {
				home_view.addView(customView.getView());
			}
			
			break;
			
		default:
			break;
		}
	}
	
	public static void changeStatusViewDisplay(int idView) {
		switch (idView) {
		case VIEWPLAYER:
			viewDisplay = VIEWPLAYER;
			home_footer_show.setBackgroundResource(R.drawable.bg_footer_home);
			home_footer_custom.setBackgroundResource(R.drawable.bg_footer_home);
			home_footer_favourie.setBackgroundResource(R.drawable.bg_footer_home);
			break;
		case VIEWSHOW:
			viewDisplay = VIEWSHOW;
			home_footer_show.setBackgroundResource(R.drawable.bg_footer_home_pressed);
			home_footer_custom.setBackgroundResource(R.drawable.bg_footer_home);
			home_footer_favourie.setBackgroundResource(R.drawable.bg_footer_home);
			break;
			
		case VIEWFAVOURITE:
			viewDisplay = VIEWFAVOURITE;
			home_footer_show.setBackgroundResource(R.drawable.bg_footer_home);
			home_footer_custom.setBackgroundResource(R.drawable.bg_footer_home);
			home_footer_favourie.setBackgroundResource(R.drawable.bg_footer_home_pressed);
			break;
		case VIEWCUSTOM:
			viewDisplay = VIEWCUSTOM;
			home_footer_show.setBackgroundResource(R.drawable.bg_footer_home);
			home_footer_custom.setBackgroundResource(R.drawable.bg_footer_home_pressed);
			home_footer_favourie.setBackgroundResource(R.drawable.bg_footer_home);
			break;

		default:
			break;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			if(viewDisplay != 0) {
				
				displayPlayer();
				changeStatusViewDisplay(VIEWPLAYER);
				
			} else {
				finish();			}
			return true;
			
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	public void displayPlayer() {
		CuaSoTinhYeu.home_view.removeAllViews();
		if(CuaSoTinhYeu.playerView.isCreate)
			CuaSoTinhYeu.home_view.addView(CuaSoTinhYeu.playerView.getView());
		else
			CuaSoTinhYeu.home_view.addView(CuaSoTinhYeu.playerView.createView(this,null));
		
		CuaSoTinhYeu.changeStatusViewDisplay(CuaSoTinhYeu.VIEWPLAYER);
		
	}
	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (Clock.isFinish || 
				(PLayerService.isRun && !PLayerService.isPlaying && !PLayerService.isPreparing)) {
			stopService(PlayerView.playerService);
			cancelNotification();
			Clock.isFinish = false;
		}
	}
	
	// -- Cancel Notification
	public static void cancelNotification() {
		String notificationServiceStr = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(notificationServiceStr);
		mNotificationManager.cancel(PLayerService.NOTIFICATION_ID);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this);
	}
}
